package com.cloud.aiservice.config;

import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.client.reactive.ReactiveElasticsearchClient;
import org.springframework.data.elasticsearch.client.reactive.ReactiveRestClients;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;
import org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ReactiveElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
import org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter;
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
import org.springframework.web.reactive.function.client.ExchangeStrategies;

@Configuration
public class ElasticSearchConfig extends AbstractElasticsearchConfiguration{

	@Override
	public RestHighLevelClient elasticsearchClient() {
		final ClientConfiguration clientConfiguration=ClientConfiguration.builder().connectedTo("localhost:9200").build();
		return RestClients.create(clientConfiguration).rest();
	}

	  @Bean
	  public ReactiveElasticsearchClient client() {

	    ClientConfiguration clientConfiguration = ClientConfiguration.builder()   
	      .connectedTo("localhost:9200", "localhost:9291")
	      .withWebClientConfigurer(webClient -> {                                 
	        ExchangeStrategies exchangeStrategies = ExchangeStrategies.builder()
	            .codecs(configurer -> configurer.defaultCodecs()
	                .maxInMemorySize(-1))
	            .build();
	        return webClient.mutate().exchangeStrategies(exchangeStrategies).build();
	       })
	      .build();

	    return ReactiveRestClients.create(clientConfiguration);
	  }
	
	
	
	  @Bean 
	  public ElasticsearchConverter elasticsearchConverter() {
	    return new MappingElasticsearchConverter(elasticsearchMappingContext());
	  }
	  @Bean 
	  public SimpleElasticsearchMappingContext elasticsearchMappingContext() {
	    return new SimpleElasticsearchMappingContext();
	  }
	  @Bean 
	  public ReactiveElasticsearchOperations reactiveElasticsearchOperations() {
	    return new ReactiveElasticsearchTemplate(client(), elasticsearchConverter());
	  }
	
}
